<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="../common/head.jsp" %>
<div class="right_col" role="main">
    <form class="form-horizontal form-label-left" method="post" action="/admin/flatform/imCommodity/insert"
          onsubmit="return checkall()">
        <span class="section">新增进货信息</span>
        <span style="color: red;font-size: x-large">${msg}</span>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5" width="15">商品id<span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="comId" name="comId" class="form-control input-small input-sm " type="text"
                       value="${commodity.id}" onblur="checkcomId()">
                <span id="demo2" style="color: red;"></span>
            </div>
        </div>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">商品名称<span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="comName" name="comName" class="form-control col-md-7 col-xs-5" value="${commodity.comName}"
                       onblur="checkcomName()" type="text" readonly>
                <span id="demo1" style="color: red;"></span>
            </div>
        </div>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">商品进价<span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="imPrice" name="imPrice" class="form-control col-md-7 col-xs-5" value="${commodity.imPrice}"
                       onblur="checkimPrice()" onchange="total()" type="number" readonly>
                <span id="demo4" style="color: red;"></span>
            </div>
        </div>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">数量 <span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="num" name="num" class="form-control col-md-7 col-xs-5" type="text" onblur="checknum()" onclick="total()"/>
                <span id="demo3" style="color: red;"></span>
            </div>
        </div>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">订单总价<span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="imTotal" name="imTotal" class="form-control col-md-7 col-xs-5" type="number" readonly>
                <span id="demo5" style="color: red;"></span>
            </div>
        </div>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">进货日期<span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="imDate" name="imDate" class="form-control col-md-7 col-xs-5"
                       value="<fmt:formatDate pattern="yyyy-MM-dd" value="${imCommodity.imDate}"/>" type="date" max=""/>
            </div>
        </div>
        <a onclick="javascript:history.back(-1);" class=" text-left btn btn-default" role="button">返回上一页</a>
        <button type="submit" class="btn btn-primary text-center  col-md-offset-4 col-lg-offset-4col-xl-offset-4">添加
        </button>
        <button class="btn btn-primary text-center" onclick="reset()">重置</button>
    </form>
    <div class="clearfix"></div>
</div>
<%@include file="../common/footer.jsp" %>
<script type="text/javascript">
    /*  校验商品名称*/
    function checkcomName() {
        var name = document.getElementById('comName').value;
        if (name) {
            document.getElementById("demo1").innerHTML = "";
            return true;
        } else {
            document.getElementById("demo1").innerHTML = "请输入商品名称！";
            document.getElementById('comName').focus();
            return false;
        }
    }

    /*判断输入的id是不是数字  */
    function checkcomId() {
        var comId = document.getElementById('comId').value;
        if (comId) {
            if (!isNaN(comId)) {
                window.location.href = "${pageContext.request.contextPath }/admin/flatform/imCommodity/getCommodityByComId?comId=" + comId;
                return true;
            } else {
                document.getElementById("demo2").innerHTML = "请输入正确的商品id！";
                document.getElementById('comId').value = "";
                document.getElementById('comId').focus();
                return false;
            }
        } else {
            document.getElementById("demo2").innerHTML = "请输入商品id！";
            document.getElementById('comId').focus();
            return false;
        }
    }
    /*  验证价格*/
    function checkimPrice() {
        var price = document.getElementById('imPrice').value;
        if (price) {
            if (!isNaN(price)) {
                total();
                return true;
            } else {
                document.getElementById("demo4").innerHTML = "请输入正确的售价！";
                document.getElementById('imPrice').value = "";
                document.getElementById('imPrice').focus();
                return false;
            }
        } else {
            document.getElementById("demo4").innerHTML = "请输入售价！";
            document.getElementById('imPrice').focus();
            return false;
        }
    }
    function checknum() {
        var num = document.getElementById('num').value;
        if (num) {
            if (!isNaN(num)) {
                total();
                return true;
            } else {
                document.getElementById("demo3").innerHTML = "请输入正确的商品数量！";
                document.getElementById('num').value = "";
                document.getElementById('num').focus();
                return false;
            }
        } else {
            document.getElementById("demo3").innerHTML = "请输入商品数量！";
            document.getElementById('num').focus();
            return false;
        }
    }
    /*  算总价*/
    function total() {
        var price = document.getElementById('imPrice').value;
        var num = document.getElementById('num').value;
        if (num && price) {
            var sum = parseFloat(num * price);
            document.getElementById('imTotal').value = sum;
        }
    }

    /* 校验整个表单 */
    function checkall() {
        var comName = checkcomName();
        var comId = checkcomId();
        var price = checkimPrice();
        var num = checknum();
        if (comName && comId && price && num) {
            alert("添加成功");
            return true;
        } else {
            alert("添加失败，请重新填写数据");
            reset();
            return false;
        }
    }

    function reset() {
        window.location.href = "${pageContext.request.contextPath }/admin/flatform/imCommodity/toAdd";
    }

    $(function () {
        //当前时间
        var date_now = new Date();
        //得到当前年份
        var year = date_now.getFullYear();
        //得到当前月份
        // js中获取Date中的month时，会比当前月份少一个月，所以这里需要先加1
        // 判断当前月份是否小于10，如果小于，那么就在月份的前面加一个 '0' ， 如果大于，就显示当前月份
        var month = date_now.getMonth() + 1 < 10 ? "0" + (date_now.getMonth() + 1) : (date_now.getMonth() + 1);
        //得到当前日期
        var date = date_now.getDate() < 10 ? "0" + date_now.getDate() : date_now.getDate();
        //设置input标签的max属性
        $("#imDate").attr("max", year + "-" + month + "-" + date);
    })
</script>